{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "380fc2af-a4f7-48e1-9cda-060f8b11c467",
   "metadata": {},
   "source": [
    "# 一、Numpy基础\n",
    "## 1.1、numpy的安装\n",
    "pip install numpy\n",
    "## 1.2、numpy的好处\n",
    "- 快(底层是c语言实现的)\n",
    "- 省空间(内存连续存储)\n",
    "- 简单(基于向量进行操作，不需要循环)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9aeab0ec-cae3-4b98-ba56-7d20a3138820",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'>\n",
      "[0 1 2 3 4 5 6 7 8 9] <class 'numpy.ndarray'>\n",
      "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n",
      "[ 0  2  4  6  8 10 12 14 16 18]\n",
      "list:3.2379980087280273\n",
      "ndarray:0.09796738624572754\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import numpy as np\n",
    "lst = list(range(10))\n",
    "#numpy使用的也是列表，只是它的对象是ndarray\n",
    "arr = np.arange(10)\n",
    "print(lst,type(lst))\n",
    "print(arr,type(arr))\n",
    "#list中如果要让所有的元素*2要用列表表达式\n",
    "print([x*2 for x in lst])\n",
    "print(arr*2)\n",
    "lst = list(range(10000000))\n",
    "arr = np.arange(10000000)\n",
    "t1 = time.time()#得到当前的时间\n",
    "lst2 = [x*2 for x in lst]\n",
    "print(f'list:{time.time()-t1}')\n",
    "t2 = time.time()\n",
    "arr2 = arr*2\n",
    "print(f'ndarray:{time.time()-t2}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1e53cb6-3365-47d3-a541-4837590b6823",
   "metadata": {},
   "source": [
    "## 1.3、创建numpy\n",
    "有多种方式可以创建numpy\n",
    "### 1.3.1、基于list的方式创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bb7093a2-2e59-4980-8fb6-fab537fa84dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'> <class 'numpy.ndarray'>\n",
      "[1 2 3 4 5] [10 20 30 40 50]\n",
      "[10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "lst = [1,2,3,4,5]\n",
    "a1 = np.array(lst)\n",
    "a2 = np.array([10,20,30,40,50])\n",
    "#不建议使用下面的方式\n",
    "a3 = np.array([range(10,20)])\n",
    "print(type(a1),type(a2))\n",
    "print(a1,a2)\n",
    "#第一个参数是start,第二个是end(不包括最后一个),第三个参数是step\n",
    "a3 = np.arange(10,50,2)\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2d4b9429-bc59-4484-8fc9-f59aaf8ab780",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "[[ 0  1  2  3  4  5  6  7  8  9]\n",
      " [10 11 12 13 14 15 16 17 18 19]\n",
      " [20 21 22 23 24 25 26 27 28 29]\n",
      " [30 31 32 33 34 35 36 37 38 39]\n",
      " [40 41 42 43 44 45 46 47 48 49]\n",
      " [50 51 52 53 54 55 56 57 58 59]\n",
      " [60 61 62 63 64 65 66 67 68 69]\n",
      " [70 71 72 73 74 75 76 77 78 79]\n",
      " [80 81 82 83 84 85 86 87 88 89]\n",
      " [90 91 92 93 94 95 96 97 98 99]]\n",
      "[[0 1]\n",
      " [2 3]\n",
      " [4 5]\n",
      " [6 7]\n",
      " [8 9]]\n",
      "[[ 0]\n",
      " [ 1]\n",
      " [ 2]\n",
      " [ 3]\n",
      " [ 4]\n",
      " [ 5]\n",
      " [ 6]\n",
      " [ 7]\n",
      " [ 8]\n",
      " [ 9]\n",
      " [10]\n",
      " [11]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a1 = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "print(a1)\n",
    "#先创建一维的数组，之后使用reshape来改变形状\n",
    "a2 = np.arange(100).reshape(10,10)\n",
    "print(a2)\n",
    "#-1表示自动计算行或者列\n",
    "#2列自动计算行\n",
    "a3 = np.arange(10).reshape(-1,2)\n",
    "print(a3)\n",
    "#变成一个列\n",
    "a4 = np.arange(12).reshape(-1,1)\n",
    "print(a4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbd89a84-34e2-4e4e-9d92-de90febe86b6",
   "metadata": {},
   "source": [
    "### 1.3.2、其他的创建方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9d3cfa2f-85b9-4df3-aa2f-1108ae879525",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
      "[[0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0.]]\n",
      "****************************************************************************************************\n",
      "[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      "[[1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1.]]\n",
      "****************************************************************************************************\n",
      "[[6 6 6 6 6]\n",
      " [6 6 6 6 6]\n",
      " [6 6 6 6 6]\n",
      " [6 6 6 6 6]\n",
      " [6 6 6 6 6]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#创建一个数组，用0来填充\n",
    "a1 = np.zeros(10)\n",
    "print(a1)\n",
    "a2 = np.zeros((5,5))\n",
    "print(a2)\n",
    "print(\"*\"*100)\n",
    "#使用1来填充\n",
    "a3 = np.ones(10)\n",
    "a4 = np.ones((6,6))\n",
    "print(a3)\n",
    "print(a4)\n",
    "print(\"*\"*100)\n",
    "#用第二参数的值来填充\n",
    "a5 = np.full((5,5),6)\n",
    "print(a5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "eb1f6ddd-2c8d-4dee-868d-1e4a6e78222d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]\n",
      "[[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n",
      "[0.28933137 0.65913901 0.4845265  0.46450281 0.21819268 0.10217432\n",
      " 0.37687473 0.67788292 0.28247148 0.09655085]\n",
      "[[0.64204116 0.66082097 0.43162047 0.70391838 0.84648649]\n",
      " [0.44909365 0.28523048 0.74251691 0.73366711 0.8038567 ]\n",
      " [0.97822417 0.84147739 0.45750077 0.6417087  0.84036126]\n",
      " [0.2856369  0.93714537 0.6499545  0.01116596 0.70912371]\n",
      " [0.98439729 0.50768557 0.94103975 0.97377182 0.57438048]]\n",
      "[[65 52 74 77]\n",
      " [95 72 29 59]\n",
      " [80 76 73 34]]\n",
      "[ 0.34516757 -0.51070753  0.41178037  0.56414839 -0.47205386  0.87078705\n",
      " -0.22194397  0.30403945  1.22541221 -0.04649612  0.34885208  0.4844325\n",
      "  1.17530499 -0.39789433  1.14479548 -0.97511328 -1.18541513  0.57286986\n",
      "  0.02017764  0.14549074]\n",
      "[ 62.46750877  66.22821769 102.46839155 107.26669219  94.92971398\n",
      " 111.07162486  95.83812152  84.10794807 115.31485936  97.38907945\n",
      " 101.59188811 102.83151069 116.47341176  68.56245508  98.10170313\n",
      "  79.73388568  85.29860523  95.79723951 101.39026687 101.67672339\n",
      "  99.5886656  103.13701668  92.63369681 116.92044812 114.21107513\n",
      "  91.72993128 112.37194944 120.50514425  91.43180435  78.28740481\n",
      " 103.09032115  74.63316917  76.93287588  82.58892031  90.83421773\n",
      " 102.74184187  91.95746772  99.19146485 111.84190189  92.39617924\n",
      "  91.85867913 132.00155884 108.4600681  108.63491449 126.82467625\n",
      "  92.92357022 118.22800935 100.24178797  95.96515398  77.3791    ]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#等差数列，个数要比理解中的数+1\n",
    "a1 = np.linspace(0,10,11)\n",
    "print(a1)\n",
    "#单位矩阵，第三个参数是k，表示1的第一个位置\n",
    "a2 = np.eye(3,3)\n",
    "print(a2)\n",
    "#生成一个一维的随机数\n",
    "a3 = np.random.rand(10)\n",
    "print(a3)\n",
    "#生成一个5*5的二维数组\n",
    "a4 = np.random.rand(5,5)\n",
    "print(a4)\n",
    "#生成随机的整数，大小是3*4\n",
    "a5 = np.random.randint(20,100,(3,4))\n",
    "print(a5)\n",
    "#生成正太分布的数据,randn表示均值是0，标准差是1\n",
    "a6 = np.random.randn(20)\n",
    "print(a6)\n",
    "#均值是100，标准差是15，20表示20个数\n",
    "a7 = np.random.normal(100,15,50)\n",
    "print(a7)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "632e2a04-1c0e-4404-8eec-069e5ffe7887",
   "metadata": {},
   "source": [
    "## 1.4、numpy的基本属性和方法\n",
    "### 1.4.1、常用的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d08df5c0-180e-43f9-b473-c7df767f263f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.41769165 0.61232707 0.38292788 0.54867878 0.97299623]\n",
      " [0.36474826 0.33728886 0.04237473 0.18013044 0.06497205]\n",
      " [0.13588854 0.3377708  0.23179224 0.18197087 0.79827644]]\n",
      "(3, 5)\n",
      "float64\n",
      "15\n",
      "2\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.rand(3,5)\n",
    "print(arr)\n",
    "#显示数组的形状\n",
    "print(arr.shape)\n",
    "#输出数据类型\n",
    "print(arr.dtype)\n",
    "#元素的个数\n",
    "print(arr.size)\n",
    "#数组的维度\n",
    "print(arr.ndim)\n",
    "#每个元素的大小\n",
    "print(arr.itemsize)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61dbc114-0aad-4fc2-bc70-5ae04d7fff0d",
   "metadata": {},
   "source": [
    "### 1.4.2、常用的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8fbda0b4-ea6b-4497-a049-76319fe76544",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[11 10 36 57 65  8 22 17 40  7]\n",
      " [33 41 92 20 14 31 63 54 17 89]\n",
      " [77 37 40 40 95  9 47 45 13 57]\n",
      " [29  9 80 84 82 66 67  7 26 73]\n",
      " [53 90 96 30 80 33 86 39 93  6]\n",
      " [57 56 20 84 40 35 41 47 65  4]\n",
      " [79 40 86 14 66 99 47 44 61 76]\n",
      " [ 2 18 13  9 77 28 46 82 78 14]\n",
      " [15 21 48 14 64 90 92 88 31 69]\n",
      " [43 57 28 19 47  7 66 61 85 20]]\n",
      "****************************************************************************************************\n",
      "2 99 47.09\n",
      "27.3\n",
      "[ 7  8 10 11 17 22 36 40 57 65]\n",
      "****************************************************************************************************\n",
      "[[5 2 4 1]\n",
      " [6 7 2 1]\n",
      " [3 4 1 2]]\n",
      "[5 2 4 1]\n",
      "[[3 1 2 0]\n",
      " [3 2 0 1]\n",
      " [2 3 0 1]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.randint(1,100,(10,10))\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#获取最大值和最小值,平均值\n",
    "print(arr.min(),arr.max(),arr.mean())\n",
    "#第一行的平均值\n",
    "print(arr[0].mean())\n",
    "arr2 = np.sort(arr)#自动排序每一行\n",
    "# print(arr2)\n",
    "arr3 = np.sort(arr[0])\n",
    "print(arr3)\n",
    "arr = np.array([[5,2,4,1],[6,7,2,1],[3,4,1,2]])\n",
    "print(\"*\"*100)\n",
    "print(arr)\n",
    "#获取下标的排序号\n",
    "print(arr[0])\n",
    "print(np.argsort(arr))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0929a78f-a420-4b81-b1dd-0f805df70259",
   "metadata": {},
   "source": [
    "## 1.5、基本的数据类型\n",
    "| 类型      | 精度     | 占字节          | 示例     |\n",
    "| ------- | ------ | ------------ | ------ |\n",
    "| int8    | 整数，1字节 | -128~127     | 灰度图像像素 |\n",
    "| int16   | 整数，2字节 | -32768~32767 | 音频信号   |\n",
    "| int32   | 整数，4字节 | 大多数整数运算      |        |\n",
    "| float32 | 小数，4字节 | GPU/深度学习常用   |        |\n",
    "| float64 | 小数，8字节 | 默认值（高精度）     |        |\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "55b7bac7-8af2-4d46-a5e0-6318b1a262e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1 1 1]] int32 528\n",
      "928\n",
      "328\n",
      "528\n",
      "[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sys\n",
    "a1 = np.ones((10,10),dtype=np.int32)\n",
    "print(a1,a1.dtype,sys.getsizeof(a1))\n",
    "a2 = np.ones((10,10))\n",
    "print(sys.getsizeof(a2))\n",
    "a3 = np.ones((10,10),dtype=np.int16)\n",
    "print(sys.getsizeof(a3))\n",
    "#astype可以修改数据类型\n",
    "a4 = a3.astype(np.float32)\n",
    "print(sys.getsizeof(a4))\n",
    "print(a4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c67fcaf-99f8-4647-9327-34c4b565ed68",
   "metadata": {},
   "source": [
    "# 二、numpy的查询操作\n",
    "## 2.1、切片\n",
    "### 2.2.1、切片基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "01fcb4bf-9d9b-4505-b454-38a3d1deb409",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[1 2 3 4]\n",
      "[1 3]\n",
      "[9 8 7 6 5 4 3 2 1 0]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.arange(10)\n",
    "print(arr)\n",
    "#[]表示切片，第一个表示start，第二个表示end(不包含)，第三个表示步长\n",
    "print(arr[1:5])\n",
    "print(arr[1:5:2])\n",
    "print(arr[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "42f15118-24eb-4bdd-927a-69e0b1e78d78",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[81 92 41 24 39 64]\n",
      " [93  3 49 82 21 54]\n",
      " [37 42 65 30 92 15]\n",
      " [18 11 86 54 67 69]\n",
      " [36 85 25 33 14 93]\n",
      " [ 4 20 70 77 14 60]]\n",
      "****************************************************************************************************\n",
      "[[93  3 49 82 21 54]\n",
      " [37 42 65 30 92 15]]\n",
      "[[92 41 24]\n",
      " [ 3 49 82]\n",
      " [42 65 30]\n",
      " [11 86 54]\n",
      " [85 25 33]\n",
      " [20 70 77]]\n",
      "[93  3 49 82 21 54]\n",
      "[41 49 65 86 25 70]\n",
      "[[41]\n",
      " [49]\n",
      " [65]\n",
      " [86]\n",
      " [25]\n",
      " [70]]\n",
      "[[92 41 24]\n",
      " [42 65 30]\n",
      " [85 25 33]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.randint(1,100,(6,6))\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#行切片，获取下标为1和2的两行\n",
    "print(arr[1:3])\n",
    "#列切片,获取2,3,4四列\n",
    "print(arr[:,1:4])\n",
    "#获取第二行\n",
    "print(arr[1])\n",
    "#此时会进行降维\n",
    "print(arr[:,2])\n",
    "#如果只想获取一列，不降维，需要在列中进行分割\n",
    "print(arr[:,2:3])\n",
    "#取出所有的偶数行，显示1->3列的信息\n",
    "print(arr[::2,1:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a2d0732-192b-4655-8df2-3d94bd497819",
   "metadata": {},
   "source": [
    "### 2.1.2、切片练习\n",
    "- 创建一个1-20的连续数的4*5的二维数组\n",
    "- 取出第 2 行；\n",
    "- 取出第 3 列；\n",
    "- 取出前两行、后三列；\n",
    "- 取出右下角的 2×2 子矩阵；\n",
    "- 取出奇数行（第 1、3 行）；\n",
    "- 取出偶数列（第 0、2、4 列）；\n",
    "- 将矩阵的每一行倒序；\n",
    "- 将矩阵的行顺序完全倒序；\n",
    "- 取出前两行并将其展平成一维"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8818affa-192d-43ac-ac6f-11a78a3ce29c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5]\n",
      " [ 6  7  8  9 10]\n",
      " [11 12 13 14 15]\n",
      " [16 17 18 19 20]]\n",
      "****************************************************************************************************\n",
      "[ 6  7  8  9 10]\n",
      "[[ 3]\n",
      " [ 8]\n",
      " [13]\n",
      " [18]]\n",
      "[[ 3  4  5]\n",
      " [ 8  9 10]]\n",
      "[[14 15]\n",
      " [19 20]]\n",
      "[[ 6  7  8  9 10]\n",
      " [16 17 18 19 20]]\n",
      "[[ 1  3  5]\n",
      " [ 6  8 10]\n",
      " [11 13 15]\n",
      " [16 18 20]]\n",
      "[[ 5  4  3  2  1]\n",
      " [10  9  8  7  6]\n",
      " [15 14 13 12 11]\n",
      " [20 19 18 17 16]]\n",
      "[[16 17 18 19 20]\n",
      " [11 12 13 14 15]\n",
      " [ 6  7  8  9 10]\n",
      " [ 1  2  3  4  5]]\n",
      "[[ 1  2  3  4  5  6  7  8  9 10]]\n",
      "[[ 1]\n",
      " [ 2]\n",
      " [ 3]\n",
      " [ 4]\n",
      " [ 5]\n",
      " [ 6]\n",
      " [ 7]\n",
      " [ 8]\n",
      " [ 9]\n",
      " [10]]\n",
      "[[ 1  2  3  4  5]\n",
      " [16 17 18 19 20]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#创建一个1-20的连续数的4*5的二维数组\n",
    "arr = np.arange(1,21).reshape(4,5)\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#取出第 2 行；\n",
    "print(arr[1])\n",
    "#取出第 3 列；\n",
    "print(arr[:,2:3])\n",
    "#取出前两行、后三列；\n",
    "print(arr[0:2,2:])\n",
    "#取出右下角的 2×2 子矩阵；\n",
    "print(arr[-2:,-2:])\n",
    "#取出奇数行（第 1、3 行）；\n",
    "print(arr[1::2])\n",
    "#取出偶数列（第 0、2、4 列）；\n",
    "print(arr[:,::2])\n",
    "#将矩阵的每一行倒序；\n",
    "print(arr[:,::-1])\n",
    "#将矩阵的行顺序完全倒序；\n",
    "print(arr[::-1])\n",
    "#取出前两行并将其展平成一维\n",
    "print(arr[0:2].reshape(1,-1))\n",
    "print(arr[0:2].reshape(-1,1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4e3a6ce-83bd-4538-b184-7b926f76eeee",
   "metadata": {},
   "source": [
    "## 2.2、索引\n",
    "### 2.2.1、花式索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "22e07740-3bc3-49e9-b3e6-9959fc70e64d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9 3 9 7 7 4 7 5 6 8]\n",
      "[9 7 7]\n",
      "[9 4]\n",
      "[7 7 3]\n",
      "****************************************************************************************************\n",
      "[[ 1  2  3  4  5]\n",
      " [ 6  7  8  9 10]\n",
      " [11 12 13 14 15]\n",
      " [16 17 18 19 20]\n",
      " [21 22 23 24 25]]\n",
      "****************************************************************************************************\n",
      "[[ 6  7  8  9 10]\n",
      " [21 22 23 24 25]\n",
      " [11 12 13 14 15]]\n",
      "[[ 3  2  4]\n",
      " [ 8  7  9]\n",
      " [13 12 14]\n",
      " [18 17 19]\n",
      " [23 22 24]]\n",
      "[ 2 15  3]\n",
      "****************************************************************************************************\n",
      "[[ 9  8 10]\n",
      " [24 23 25]\n",
      " [14 13 15]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# arr = np.arange(10)\n",
    "arr = np.random.randint(1,10,10);\n",
    "print(arr)\n",
    "print(arr[2:5])#切片，选择了2-4的元素“\n",
    "print(arr[[2,5]])\n",
    "idx = [3,4,1]\n",
    "print(arr[idx])\n",
    "print(\"*\"*100)\n",
    "arr = np.arange(1,26).reshape(5,-1)\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "print(arr[[1,4,2]])\n",
    "print(arr[:,[2,1,3]])\n",
    "#行列配对\n",
    "#此时的结果不是矩阵，而是配对(0,1)和(2,4)\n",
    "print(arr[[0,2,0],[1,4,2]])\n",
    "print(\"*\"*100)\n",
    "print(arr[np.ix_([1,4,2],[3,2,4])])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53acb60e-51ff-4192-9150-3c6033fd2ae8",
   "metadata": {},
   "source": [
    "### 2.2.2、布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "19dcc2a8-e474-470e-8777-29492b218595",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 5]\n",
      "[False False  True  True  True]\n",
      "[False False  True False False  True False False  True False False  True\n",
      " False False  True False False  True False]\n",
      "[ 3  6  9 12 15 18]\n",
      "[13 25  6  3 11  9 71 60 75 49 37 45 56 59 12 88 98 69 31 62]\n",
      "[71 60 75 56 59 88 98 69 62]\n",
      "[ 6 60 56 12 88 98 62]\n",
      "[ 6  3  9 71 60 75 45 56 59 12 88 98 69 62]\n",
      "[25 37 31]\n",
      "[13 25  6  3 11  9 49 37 45 12 31]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.array([1,2,3,4,5])\n",
    "idb = [True,True,False,False,True]\n",
    "print(arr[idb])\n",
    "#此时mask就是一个布尔数组\n",
    "mask = arr>2\n",
    "print(mask)\n",
    "arr = np.arange(1,20)\n",
    "#得到的是一个数组，数组里面会把被3整除的数设置为True，其他为False\n",
    "print(arr%3==0)\n",
    "print(arr[arr%3==0])\n",
    "#创建一个1-100随机整数的20个元素的数组\n",
    "arr = np.random.randint(1,100,20)\n",
    "print(arr)\n",
    "print(arr[arr>=50])\n",
    "print(arr[arr%2==0])\n",
    "#或运算\n",
    "print(arr[(arr>=50)|(arr%3==0)])\n",
    "#与运算\n",
    "print(arr[(arr>=20)&(arr<=40)])\n",
    "#非运算\n",
    "print(arr[~(arr>50)])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62a878c1-5c77-4ca4-bfa7-19862d1d0f4e",
   "metadata": {},
   "source": [
    "## 2.3、操作练习\n",
    "### 2.3.3、索引练习\n",
    "- **基础题：**\n",
    "    - 取出第 1、3 行。  \n",
    "    - 取出第 0、2、3 行的第 2 列。  \n",
    "    - 取出第 0、2 行与第 1、3 列组成的矩形区域。  \n",
    "    - 取出 (0,1)、(1,3)、(3,0) 位置的元素。  \n",
    "    - 将第 1、3 行调换顺序输出。  \n",
    "***\n",
    "- **进阶题：**\n",
    "    - 取所有行的第 0、3 列。  \n",
    "    - 取第 0、2、3 行，且每行只取第 1、2 列。\n",
    "    - 倒序行输出上面的矩阵 \n",
    "    - 对第 0、3 行按列倒序输出。  \n",
    "    - 将数组的第 0、3 行互换位置（可一步实现）。  \n",
    "***\n",
    "\n",
    "  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8ebe8790-93b6-4338-be17-0adbc4d127a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]\n",
      " [13 14 15 16]]\n",
      "****************************************************************************************************\n",
      "[[ 5  6  7  8]\n",
      " [13 14 15 16]]\n",
      "[[ 3]\n",
      " [11]\n",
      " [15]]\n",
      "[[ 2  4]\n",
      " [10 12]]\n",
      "[ 2  8 13]\n",
      "[[ 1  2  3  4]\n",
      " [13 14 15 16]\n",
      " [ 9 10 11 12]\n",
      " [ 5  6  7  8]]\n",
      "****************************************************************************************************\n",
      "[[ 1  4]\n",
      " [ 5  8]\n",
      " [ 9 12]\n",
      " [13 16]]\n",
      "****************************************************************************************************\n",
      "[[ 2  3]\n",
      " [10 11]\n",
      " [14 15]]\n",
      "****************************************************************************************************\n",
      "[[14 15]\n",
      " [10 11]\n",
      " [ 2  3]]\n",
      "[[ 4  3  2  1]\n",
      " [16 15 14 13]]\n",
      "[[ 4  2  3  1]\n",
      " [ 8  6  7  5]\n",
      " [12 10 11  9]\n",
      " [16 14 15 13]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.arange(1,17).reshape(4,-1)\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#取出第 1、3 行。\n",
    "print(arr[[1,3]])\n",
    "#取出第 0、2、3 行的第 2 列。\n",
    "print(arr[[0,2,3],2].reshape(-1,1))\n",
    "#取出第 0、2 行与第 1、3 列组成的矩形区域。\n",
    "print(arr[np.ix_([0,2],[1,3])])\n",
    "#取出 (0,1)、(1,3)、(3,0) 位置的元素。\n",
    "print(arr[[0,1,3],[1,3,0]])\n",
    "#将第 1、3 行调换顺序输出。\n",
    "print(arr[[0,3,2,1]])\n",
    "print(\"*\"*100)\n",
    "#取所有行的第 0、3 列。\n",
    "print(arr[:,[0,3]])\n",
    "#取第 0、2、3 行，且每行只取第 1、2 列。\n",
    "print(\"*\"*100)\n",
    "print(arr[np.ix_([0,2,3],[1,2])])\n",
    "#倒序行输出上面的矩阵\n",
    "print(\"*\"*100)\n",
    "print(arr[np.ix_([0,2,3],[1,2])][::-1])\n",
    "#对第 0、3 行按列倒序输出。\n",
    "print(arr[[0,3],::-1])\n",
    "#将数组的第 0、3 列互换位置（可一步实现）。\n",
    "print(arr[:,[3,1,2,0]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7f31110-55f7-4004-b70a-ffa9ca9a03a7",
   "metadata": {},
   "source": [
    "### 2.3.4、布尔索引练习\n",
    "\n",
    "- **基础题：**\n",
    "    - 取出所有大于 10 的元素。  \n",
    "    - 取出所有偶数。  \n",
    "    - 取出所有小于 8 的元素。  \n",
    "    - 取出所有能被 3 整除的元素。  \n",
    "***\n",
    "- **进阶题：**\n",
    "    - 取出所有大于 5 且小于 12 的元素。  \n",
    "    - 取出所有奇数或能被 4 整除的数。  \n",
    "    - 取出所有不在 5 到 12 范围内的元素。  \n",
    "    - 取出前两行中所有偶数。  \n",
    "    - 取出第 0、2 行中大于 10 的元素。  \n",
    "    - 取出第 1～3 行中第 1、3 列的所有元素，并只保留能被 3 整除的数。  \n",
    "***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "12ff8f73-a6a3-465b-8c2f-a641e761e7d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]\n",
      " [13 14 15 16]]\n",
      "****************************************************************************************************\n",
      "[11 12 13 14 15 16]\n",
      "[ 2  4  6  8 10 12 14 16]\n",
      "[1 2 3 4 5 6 7]\n",
      "[ 3  6  9 12 15]\n",
      "****************************************************************************************************\n",
      "[ 6  7  8  9 10 11]\n",
      "[ 1  3  4  5  7  8  9 11 12 13 15 16]\n",
      "[ 1  2  3  4  5 12 13 14 15 16]\n",
      "[2 4 6 8]\n",
      "[2 4 6 8]\n",
      "[11 12]\n",
      "[11 12]\n",
      "[ 6 12]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.arange(1,17).reshape(4,-1)\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#取出所有大于 10 的元素。\n",
    "print(arr[arr>10])\n",
    "#取出所有偶数。\n",
    "print(arr[arr%2==0])\n",
    "#取出所有小于 8 的元素。\n",
    "print(arr[arr<8])\n",
    "#取出所有能被 3 整除的元素。\n",
    "print(arr[arr%3==0])\n",
    "print(\"*\"*100)\n",
    "\n",
    "arr = np.arange(1,17).reshape(-1,4)\n",
    "#取出所有大于 5 且小于 12 的元素。\n",
    "print(arr[(arr>5)&(arr<12)])\n",
    "#取出所有奇数\\或能被 4 整除的数。\n",
    "print(arr[(arr%2!=0)|(arr%4==0)])\n",
    "#取出所有不在 5 到 12 范围内的元素。\n",
    "print(arr[~((arr>5)&(arr<12))])\n",
    "#取出前两行中所有偶数。\n",
    "print(arr[:2][arr[:2]%2==0])\n",
    "#可以分开写\n",
    "arr2 = arr[:2]\n",
    "print(arr2[arr2%2==0])\n",
    "#取出第 0、2 行中大于 10 的元素。\n",
    "print(arr[[0,2]][arr[[0,2]]>10])\n",
    "arr2 = arr[[0,2]]\n",
    "print(arr2[arr2>10])\n",
    "#取出第 1～3 行中第 1、3 列的所有元素，并只保留能被 3 整除的数。\n",
    "# arr2 = arr[np.ix_([1,2,3],[1,3])]\n",
    "arr2 = arr[1:4][:,[1,3]]\n",
    "print(arr2[arr2%3==0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f409f2ef-26f9-40d3-ac95-4308098e128d",
   "metadata": {},
   "source": [
    "### 2.3.5、混合练习（花式 + 布尔）\n",
    "\n",
    "- **综合题：**\n",
    "    - 取第 1、3 行中所有大于 8 的元素。\n",
    "    - 取第 0、3 行中大于 5 的元素\n",
    "    - 取第 1、2 行中能被 4 整除的数。 \n",
    "    - 取第 0、2 行的第 1、3 列，再筛选出其中大于 5 的元素。\n",
    "    - 取出第 1～3 行、第 2～5 列中所有大于 10 且为偶数的元素。\n",
    "    - 取出第 0、2、3 行中列索引为 [1,3,4] 的所有元素，再筛选能被 3 整除的数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "555f7628-9a97-444a-8da3-1af62dd3992d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5  6]\n",
      " [ 7  8  9 10 11 12]\n",
      " [13 14 15 16 17 18]\n",
      " [19 20 21 22 23 24]]\n",
      "****************************************************************************************************\n",
      "[ 9 10 11 12 19 20 21 22 23 24]\n",
      "[ 6 19 20 21 22 23 24]\n",
      "[ 8 12 16]\n",
      "[14 16]\n",
      "[11 12 15 16 17 18 21 22 23 24]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.arange(1,25).reshape(4,-1)\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#取第 1、3 行中所有大于 8 的元素。\n",
    "print(arr[[1,3]][arr[[1,3]]>8])\n",
    "#取第 0、3 行中大于 5 的元素\n",
    "arr2 = arr[[0,3]]\n",
    "print(arr2[arr2>5])\n",
    "#取第 1、2 行中能被 4 整除的数。\n",
    "print(arr[1:3][arr[1:3]%4==0])\n",
    "#取第 0、2 行的第 1、3 列，再筛选出其中大于 5 的元素。\n",
    "arr2 = arr[np.ix_([0,2],[1,3])]\n",
    "print(arr2[arr2>5])\n",
    "#取出第 1～3 行、第 2～5 列中所有大于 10 且为偶数的元素。\n",
    "arr2 = arr[1:4,2:6]\n",
    "print(arr2[arr2>10])\n",
    "#取出第 0、2、3 行中列索引为 [1,3,4] 的所有元素，再筛选能被 3 整除的数。\n",
    "arr2 = arr[np.ix_([0,2,3],[1,3,4])]\n",
    "print(arr2[arr2%3==0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91fed811-84ff-4089-861d-2792bb852979",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
